<!--Carga de datos-->
<% 
    # Nos traemos los datos para rellenar la tabla
    @cfds = CustomFieldDependent
      .where(:project_id => @project.id).
      joins([:conditioning_custom_field, :conditioned_custom_field])
      .order('conditioning_value, custom_fields.name ,conditioned_custom_fields_custom_field_dependents.name asc')

    # Creamos el objeto y lo cargamos de valores si existen
    @cfd = CustomFieldDependent.new
    
    # Accedemos a los campos personalizados disponibles para esta pantalla
    # para ofrecerlos como condicionados
    @cfs_all = CustomField.where("is_for_all=?",true) |  Project.find(@project.id).issue_custom_fields

    # Creamos un subconjunto con los que son tipo lista
    @cfs_lists = []
    @cfs_all.each do |current_cf|
      if current_cf.field_format == 'list'
        @cfs_lists.push(current_cf)
      end 
    end
%>

<!-- Carga de javascript para la carga de campos-->
<script type="text/javascript">
// <![CDATA[
<%= raw "cfsAll=#{@cfs_all.to_json}" %>
<%= raw "cfsList=#{@cfs_lists.to_json}" %>
p_cfd_conditioning_custom_field = <%= (not params[:p_cfd_conditioning_custom_field].nil?) ? params[:p_cfd_conditioning_custom_field] : 'null' %>
p_cfd_conditioning_value = <%= raw (not params[:p_cfd_conditioning_value].nil?) ? "'"+params[:p_cfd_conditioning_value]+"'" : 'null' %>
p_cfd_conditioned_custom_field = <%= (not params[:p_cfd_conditioned_custom_field].nil?) ? params[:p_cfd_conditioned_custom_field] : 'null' %>
//]]>
</script>
<%= javascript_include_tag('settings_custom_field_dependent.js', :plugin => :custom_field_dependent) %>

<%= error_messages_for 'cfd' %>
<% unless @cfds.nil? or @cfds.empty? %>
  <table class='list'>
    <tr>
      <th><%=l(:conditioning_custom_field)%></th>
      <th><%=l(:conditioning_value)%></th>
      <th><%=l(:conditioned_custom_field)%></th>
      <th></th>
    </tr>
      <% for cfd in @cfds %>
          <tr class="<%= cycle('odd', 'even') -%>">
            <td><%= cfd.conditioning_custom_field.name %></td>
            <td><%= cfd.conditioning_value %></td>
            <td><%= cfd.conditioned_custom_field.name %></td>
            
            <td><%=link_to "", 
              {:controller => 'custom_field_dependent_project_settings',
               :action => 'delete',
               :idproject => @project.id,
               :id_cfd => cfd.id },
               :method=> 'delete',
               :class => "icon icon-del",
               :confirm => l(:conditioning_custom_field_delete_sure)%></td>
            
          </tr>
      <% end %>
  </table>
<% else %>
   <p class="nodata"><%= l(:label_no_data) %></p>
<% end %>

<!--Listado-->

<!--Formulario de alta-->
<%= form_tag(
  {:controller => 'custom_field_dependent_project_settings',
   :action => 'create',
   :idproject => @project.id},
  :method=> 'post',
  :multipart => true) do %>
    <%= render :partial => 'custom_field_dependent_project_settings/form', :locals => {cfd: @cfd}%>
    <%= submit_tag l(:button_save) %>
<% end %>